#include "ConfModule.h"
#include "Logger.h"

namespace BASE{

	template<>
	void ValueConf<int, int>::apply(){
		val = CConfig::get()->getIntValue(key, defval);
		InfoLog("bind conf key:" << key << ",val:" << val);
	}

	template<>
	void ValueConf<size_t, size_t>::apply(){
		val = CConfig::get()->getIntValue(key, defval);
		InfoLog("bind conf key:" << key << ",val:" << val);
	}

	template<>
	void ValueConf<float, float>::apply(){
		val = CConfig::get()->getDoubleValue(key, defval);
		InfoLog("bind conf key:" << key << ",val:" << val);
	}

	template<>
	void ValueConf<bool, bool>::apply(){
		val = CConfig::get()->getBoolValue(key, defval);
		InfoLog("bind conf key:" << key << ",val:" << val);
	}

	template<>
	void ValueConf<std::string, std::string>::apply(){
		val = CConfig::get()->getStringValue(key, defval);
		InfoLog("bind conf key:" << key << ",val:" << val);
	}

	BindConf::BindConf(){

	}

	BindConf::~BindConf(){
		BindConfs::iterator iter = mBindConfs.begin();
		for(; iter != mBindConfs.end(); ++iter){
			delete (*iter);
		}
		iter = mWatchConfs.end();
		for (; iter != mWatchConfs.end(); ++iter){
			delete (*iter);
		}
	}

	void BindConf::initialize(const std::string & configFileName){
		if (!CConfig::get()->loadFile(configFileName)){
			ErrLog("load config file error");
		}
	}

	void BindConf::update(){
		if (CConfig::get()->isModified()){
			BindConfs::iterator iter = mWatchConfs.begin();
			for (; iter != mWatchConfs.end(); ++iter){
				(*iter)->apply();
			}
		}
	}

	void BindConf::load(){
		BindConfs::iterator iter = mBindConfs.begin();
		for (; iter != mBindConfs.end(); ++iter){
			(*iter)->apply();
		}
	}

	void BindConf::bind(const std::string & key, size_t & v, size_t defaultVal){
		mBindConfs.push_back(new ValueConf<size_t, size_t>(key, v, defaultVal));
	}

	void BindConf::bind(const std::string & key, int & v, int defaultVal){
		mBindConfs.push_back(new ValueConf<int, int>(key, v, defaultVal));
	}

	void BindConf::bind(const std::string & key, float& v, float defaultVal) {
		mBindConfs.push_back(new ValueConf<float, float>(key, v, defaultVal));
	}

	void BindConf::bind(const std::string & key, bool& v, bool defaultVal) {
		mBindConfs.push_back(new ValueConf<bool, bool>(key, v, defaultVal));
	}

	void BindConf::bind(const std::string & key, std::string & v, const char * defaultVal) {
		mBindConfs.push_back(new ValueConf<std::string, std::string>(key, v, defaultVal));
	}

	void BindConf::watch(const std::string & key, size_t & v, size_t defaultVal) {
		bind(key, v, defaultVal);
		mWatchConfs.push_back(new ValueConf<size_t, size_t>(key, v, defaultVal));
	}

	void BindConf::watch(const std::string & key, int& v, int defaultVal) {
		bind(key, v, defaultVal);
		mWatchConfs.push_back(new ValueConf<int, int>(key, v, defaultVal));
	}

	void BindConf::watch(const std::string & key, float & v, float defaultVal) {
		bind(key, v, defaultVal);
		mWatchConfs.push_back(new ValueConf<float, float>(key, v, defaultVal));
	}

	void BindConf::watch(const std::string & key, bool & v, bool defaultVal) {
		bind(key, v, defaultVal);
		mWatchConfs.push_back(new ValueConf<bool, bool>(key, v, defaultVal));
	}

	void BindConf::watch(const std::string & key, std::string & v, const char * defaultVal) {
		bind(key, v, defaultVal);
		mWatchConfs.push_back(new ValueConf<std::string, std::string>(key, v, defaultVal));
	}
}
